import Cookies from 'js-cookie';
import {nowWithFormat, safeParseJson} from '@/utils/index';

const TokenKey = 'authToken';
const FreshTokenKey = 'FreshTokenKey';


export function getDomain(options) {
  if(location.href?.indexOf('localhost')>=0){
    return 'localhost'
  }else  if(location.href?.indexOf('192')>=0){
    return location?.hostname
  } else if (process.env.VUE_APP_ENV != 'production') {
    return 'dev.szqerp.com'
  }
  return '.szqerp.com'
}

export function getToken() {
var _domain= getDomain()
    return Cookies.get(TokenKey,{domain:_domain});
}

export function setToken(token) {
      var domain=getDomain()
    return Cookies.set(TokenKey, token, {
      domain
    });
}

export function getRefreshToken() {
    return Cookies.get(FreshTokenKey,{domain:getDomain()});
}
export function setRefreshToken(value) {
  return Cookies.set(FreshTokenKey,value||'',{
    domain: getDomain()
  });
}


export function removeToken() {
    return remoteAllCookies()
}

export function remoteAllCookies() {

  var _domains=['localhost','dev.szqerp.com','.szqerp.com','www.szqerp.com']
  _domains.forEach(str=>{
    Cookies.remove('thirdInfo',{domain:str});
    Cookies.remove('getuserinfobyauth',{domain:str});
    Cookies.remove(TokenKey,{domain:str});
    Cookies.remove('tenantType',{domain:str});
    Cookies.remove('username',{domain:str});
    Cookies.remove('tenantId',{domain:str});
    Cookies.remove('refreshToken',{domain:str});

    Cookies.remove('userid',{domain:str});
    Cookies.remove('FreshTokenKey',{domain:str});
    Cookies.remove('TokenUpdatedTime',{domain:str});
  });

  Cookies.remove('thirdInfo' );
  Cookies.remove('getuserinfobyauth' );
  Cookies.remove(TokenKey );
  Cookies.remove('tenantType' );
  Cookies.remove('username' );
  Cookies.remove('tenantId' );
  Cookies.remove('refreshToken' );

  Cookies.remove('userid' );
  Cookies.remove('FreshTokenKey' );
  Cookies.remove('TokenUpdatedTime' );

}


export function getuserinfobyauth() {
  return safeParseJson(Cookies.get('getuserinfobyauth', {domain:getDomain()}));
}
export function setuserinfobyauth(tirdInfo) {
  if (!tirdInfo) {
    Cookies.remove('getuserinfobyauth',{domain:getDomain()});
    return false;
  }
  return Cookies.set('getuserinfobyauth', tirdInfo,{domain:getDomain()});
}
export function setThirdInfo(tirdInfo) {
    if (!tirdInfo) {
        Cookies.remove('thirdInfo',{domain:getDomain()});
        return false;
    }
    return Cookies.set('thirdInfo', JSON.stringify(tirdInfo),{domain:getDomain()});
}

export function getThirdInfo() {
    return safeParseJson(Cookies.get('thirdInfo'),{domain:getDomain()});
}

// region 处理token 一个小时过期
export function getTokenUpdatedTime() {
    var time = Cookies.get('TokenUpdatedTime',{domain:getDomain()});
    return time || undefined;
}

export function setTokenUpdatedTime(value) {
  Cookies.set('TokenUpdatedTime', value||'',{domain:getDomain()});
}

export function set_SINGLE_403_ALERT(value){
    Cookies.set('SINGLE_403_ALERT', value);
}

// endregion



export function  pushAuthButton(_list=[],buttonOne){
    var index=_list?.findIndex(i2=>i2?.funUrl==buttonOne?.funUrl)
    if(index>=0){
        _list?.splice(index,1)
    }
    _list.push(buttonOne)

}




